//
//  CZStatusViewModel.swift
//  WeiBoNew
//
//  Created by 秦川 on 16/5/14.
//  Copyright © 2016年 秦川. All rights reserved.
//

import UIKit
//cell对应的ViewModel
class CZStatusViewModel: NSObject {
    
    var status: CZStatus?
    //  转发微博内容
    var retweetContent: String?
    //  转发数内容
    var retweetCountContent: String?
    //  评论数
    var commentCountContent: String?
    //  赞
    var unlikeCountContent: String?
    
    //  来源
    var sourceContent: String?
    
    init(status: CZStatus) {
        super.init()
        self.status = status
        handleRetweetContent(status)
        retweetCountContent = handleCountContent(status.reposts_count, defaultTitle: "转发")
        commentCountContent = handleCountContent(status.comments_count, defaultTitle: "评论")
        unlikeCountContent = handleCountContent(status.attitudes_count, defaultTitle: "赞")
        handleSourceContent(status)
    }
    private func handleRetweetContent(status: CZStatus){
        guard let retweet = status.retweeted_status?.text else{
            return
        }
        //        转发微博存在
        guard let name = status.retweeted_status?.user?.screen_name else{
            return
        }
        //数据拼接
        retweetContent = "@\(name):\(retweet)"
    }
    //  处理转发,评论,赞的数据
    private func handleCountContent(count: Int, defaultTitle: String) -> String {
        if count > 0 {
            
            if count >= 10000 {
                //  处理显示格式
                let result = CGFloat(count / 1000 / 10)
                let resultStr = "\(result)万"
                if resultStr.containsString(".0") {
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return resultStr
            } else {
                return "\(count)"
            }
        } else {
            return defaultTitle
        }
        
    }
    //处理来源数据
    private func handleSourceContent(status: CZStatus){
        guard let source = status.source else {
        return
        }
        if source.containsString("\">"){
        let startIndex = source.rangeOfString("\">")?.endIndex
        let endIndex = source.rangeOfString("</")?.startIndex
            if let start = startIndex,end = endIndex {
             sourceContent = source.substringWithRange(start..<end)
            
            }
        }
    
    }
}
